package com.pengpeng.rocketmq.web;

import com.pengpeng.rocketmq.consumer.FilterConsumer;
import com.pengpeng.rocketmq.producer.FilterProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 彭鹏 顺序消息
 * @date 2021/10/25.
 */
@Slf4j
@RestController
@RequestMapping("/filter")
public class FilterController {

    @Autowired
    private FilterProducer producer;

    @RequestMapping("/filterProducer")
    public Object filterProducer() throws Exception {
        int totalMessagesToSend = 20;
        for (int i = 0; i < totalMessagesToSend; i++) {
            Message msg = new Message(FilterConsumer.TOPIC,
                    "filter1",
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );
            // Set some properties.
            msg.putUserProperty("a", String.valueOf(i));
            // Send the message
            SendResult sendResult = producer.getProducer().send(msg);
            System.out.printf("%s%n", sendResult);
        }
        return "成功";
    }
}

